package com.rattsoftware.client.views;

import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.TreeLoader;
import com.extjs.gxt.ui.client.data.TreeModelReader;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.core.client.GWT;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.Setting;
import com.rattsoftware.client.widgets.SettingsPanel;

public class HomePageView extends View {

	private ContentPanel homeView;
	private LayoutContainer container;
	private SettingsPanel settingsPanel;

	private TreeStore<Setting> store;
	private TreePanel<Setting> tree;
	private TreeLoader<Setting> loader;

	public HomePageView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.Init) {
			GWT.log("Handling Init Event in HomePageView");
			initUI();
		} else if (event.getType() == AppEvents.HomeView) {
			GWT.log("Handling HomePageView Event in HomePageView");
			LayoutContainer wrapper = (LayoutContainer) Registry
					.get(MainAppView.CENTER_PANEL);
			wrapper.removeAll();
			wrapper.add(container);
			wrapper.layout();
			return;
		} else if (event.getType() == AppEvents.NavHomePage) {
			GWT.log("Handling NavHomePage Event in HomePageView");
			/*Setting f = new Setting("Users");
			if (f != null) {
				loader.addListener(Loader.Load, new LoadListener() {
					@Override
					public void loaderLoad(LoadEvent le) {
						loader.removeLoadListener(this);
					}
				});
				loader.load(f);
			}*/
		}
	}

	protected void initUI() {
		homeView = new ContentPanel();
		homeView.setHeading("System Settings");
		homeView.setIconStyle("settings");
		homeView.setAnimCollapse(false);
		homeView.addListener(Events.Expand, new Listener<ComponentEvent>() {
			public void handleEvent(ComponentEvent be) {
				Dispatcher.get().dispatch(AppEvents.NavHomePage);
			}
		});

		loader = new BaseTreeLoader<Setting>(
				new TreeModelReader<List<Setting>>());
		store = new TreeStore<Setting>(loader);
		
		Setting homeSetting = new Setting("Home");
		Setting userSetting = new Setting("Users");
		Setting permissionSetting = new Setting("Permissions");
		
		store.add(homeSetting, false);
		store.add(userSetting, false);
		store.add(permissionSetting, false);
		
		tree = new TreePanel<Setting>(store);
		
		tree.getStyle().setLeafIcon(IconHelper.createStyle("settings"));
		tree.setDisplayProperty("name");
		tree.setAutoSelect(true);
		tree.getSelectionModel().addSelectionChangedListener(
				new SelectionChangedListener<Setting>() {
					@Override
					public void selectionChanged(
							SelectionChangedEvent<Setting> se) {
						Setting f = (Setting) se.getSelection().get(0);
						GWT.log("Selected Item ***** "  + f.getName());
						
						if(f.getName().equalsIgnoreCase("users")){
							GWT.log("UserList***** "  + AppEvents.UserList);
							AppEvent evt = new AppEvent(AppEvents.UserList,f);
							Dispatcher.get().dispatch(evt);
						} else {
							GWT.log("HomePageView***** "  + AppEvents.HomeView);
							AppEvent evt = new AppEvent(AppEvents.HomeView,f);
							fireEvent(evt);
						}
					}
				});

		homeView.add(tree);

		ContentPanel west = (ContentPanel) Registry.get(MainAppView.WEST_PANEL);
		west.add(homeView);
	}

	@Override
	protected void initialize() {
		container = new LayoutContainer();

		BorderLayout layout = new BorderLayout();
		layout.setEnableState(false);
		container.setLayout(layout);

		settingsPanel = new SettingsPanel();
		container.add(settingsPanel, new BorderLayoutData(LayoutRegion.CENTER));
	}
}
